package net.begincode.service.project.impl;

import java.util.ArrayList;
import java.util.List;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcCode;
import net.begincode.bean.ProjectStage;
import net.begincode.service.project.ProjectStageService;

public class ProjectStageServiceImpl implements ProjectStageService {
	private ProjectStageServiceImpl() {
	}

	private static ProjectStageServiceImpl instance = null;

	public static ProjectStageServiceImpl getInstance() {
		if (instance == null) {
			synchronized (ProjectStageServiceImpl.class) {
				if (instance == null) {
					instance = new ProjectStageServiceImpl();
				}
			}
		}
		return instance;
	}

	@Override
	public int saveProjectStage(ProjectStage projectStage) {
		
		StringBuilder addSql = new StringBuilder();
		addSql.append(" insert into project_stage(stage_name,stage_abstract,stage_content,cdate,bc_project_id,praise,bad,view_count,check_flag) ");
		addSql.append(" values(?,?,?,now(),?,?,?,?,?) ");
		return DbTemplate.insert(addSql.toString(), new Object[]{
			projectStage.getStage_name(),projectStage.getStage_abstract(),projectStage.getStage_content(),projectStage.getBc_project_id(),projectStage.getPraise(),projectStage.getBad(),projectStage.getView_count(),projectStage.getCheck_flag()
	});
	}

	@Override
	public List<ProjectStage> selNewProStageByCodeId(String codeId, int n) {
		
		String selSql =" select ps.* from project_code_rel pcr left join project_stage ps on ps.project_stage_id = pcr.project_stage_id where pcr.bc_code_id = ? limit ?";
		return DbTemplate.queryForList(selSql, new Object[]{codeId,n}, ProjectStage.class); 
	}

	@Override
	public List<ProjectStage> selNewProStageLimitN(int n) {
		
		String selSql = " select * from project_stage  order by cdate desc limit ? ";
		return DbTemplate.queryForList(selSql, new Object[]{n}, ProjectStage.class); 
	}

	@Override
	public List<ProjectStage> selProStageByProject(String projectId) {
		
		String selSql = " select * from project_stage where bc_project_id = ? order by project_stage_id  ";
		return DbTemplate.queryForList(selSql, new Object[]{projectId}, ProjectStage.class);
	}

	@Override
	public ProjectStage selProStageById(String proStageId) {
		
		String selSql = " select * from project_stage where project_stage_id = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{proStageId}, ProjectStage.class);
	}

	@Override
	public List<ProjectStage> selProStageByParam(int projId, int proStageId,int n) {
		
		String MaxSql = " select * from project_stage where bc_project_id = ? and project_stage_id > ? limit ? ";
		List<ProjectStage> list = DbTemplate.queryForList(MaxSql, new Object[]{projId,proStageId,n}, ProjectStage.class);
		if(list.size() < n){
			String minSql = " select * from project_stage where bc_project_id = ? and project_stage_id < ? limit ? ";
			list.addAll(DbTemplate.queryForList(minSql, new Object[]{projId,proStageId,n-list.size()}, ProjectStage.class)); 
		}
		return list;
	}

	@Override
	public List<ProjectStage> selProStageByParam(ProjectStage proStage) {
		
		String selSql = " select * from project_stage ";
		return DbTemplate.queryForList(selSql, null, ProjectStage.class);
	}

	@Override
	public int updProStage(ProjectStage proStage) {
		List paraList = new ArrayList();
		
		StringBuilder updSql = new StringBuilder();
		updSql.append(" update project_stage set stage_name = ?  ");
		updSql.append(" , bc_project_id = ? ");
		paraList.add(proStage.getStage_name());
		paraList.add(proStage.getBc_project_id());
		if(proStage.getStage_abstract() != null){
			updSql.append(" , stage_abstract = ? ");
			paraList.add(proStage.getStage_abstract());
		}
		if(proStage.getStage_content() != null){
			updSql.append(" , stage_content = ? ");
			paraList.add(proStage.getStage_content());
		}
		if(proStage.getPraise() != 0){
			updSql.append(" , praise = ? ");
			paraList.add(proStage.getPraise());
		}
		if(proStage.getBad() != 0){
			updSql.append(" , bad = ? ");
			paraList.add(proStage.getBad());
		}
		if(proStage.getView_count() != 0){
			updSql.append(" , view_count = ? ");
			paraList.add(proStage.getView_count());
		}
		if(proStage.getCheck_flag() != null){
			updSql.append(" , check_flag = ? ");
			paraList.add(proStage.getCheck_flag());
		}
		updSql.append(" where project_stage_id = ? ");
		paraList.add(proStage.getProject_stage_id());
		return DbTemplate.update(updSql.toString(), paraList.toArray());
	}

	@Override
	public List<ProjectStage> selNewProStageByCodeIdCache(String codeId, int n) {
		
		String selSql =" select ps.* from project_code_rel pcr left join project_stage ps on ps.project_stage_id = pcr.project_stage_id where pcr.bc_code_id = ? limit ?";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.PROJ_CACHE, "codRelProStage"+codeId);
		if(dbEle != null){
			return (List<ProjectStage> )dbEle;
		}else{
			List<ProjectStage>  ccs = DbTemplate.queryForList(selSql, new Object[]{codeId,n}, ProjectStage.class);
			CacheUtils.addEleByParam(CacheUtils.PROJ_CACHE,"codRelProStage"+codeId, ccs);
			return ccs;
		}
	}

	@Override
	public ProjectStage selProStageByIdCache(String proStageId) {
		
		String selSql = " select * from project_stage where project_stage_id = ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.PROJ_CACHE,  "projStage"+proStageId);
		if(dbEle != null){
			return (ProjectStage)dbEle;
		}else{
			ProjectStage ccs = DbTemplate.queryForObject(selSql, new Object[]{proStageId}, ProjectStage.class);
			CacheUtils.addEleByParam(CacheUtils.PROJ_CACHE, "projStage"+proStageId, ccs);
			return ccs;
		}
	}

	@Override
	public List<ProjectStage> selProStageByParamCache(int projId,
			int proStageId, int n) {
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.PROJ_CACHE,  "projStageN"+proStageId);
		if(dbEle != null){
			return (List<ProjectStage>)dbEle;
		}else{
			String MaxSql = " select * from project_stage where bc_project_id = ? and project_stage_id > ? limit ? ";
			List<ProjectStage> list = DbTemplate.queryForList(MaxSql, new Object[]{projId,proStageId,n}, ProjectStage.class);
			if(list.size() < n){
				String minSql = " select * from project_stage where bc_project_id = ? and project_stage_id < ? limit ? ";
				list.addAll(DbTemplate.queryForList(minSql, new Object[]{projId,proStageId,n-list.size()}, ProjectStage.class)); 
			}
			CacheUtils.addEleByParam(CacheUtils.PROJ_CACHE, "projStageN"+proStageId, list);
			return list;
		}
	}

	@Override
	public List<ProjectStage> selNewProStageLimitNCache(int n) {
		
		String selSql = " select * from project_stage  order by cdate desc limit ? ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.PROJ_CACHE,  "newProStage"+n);
		if(dbEle != null){
			return (List<ProjectStage>)dbEle;
		}else{
			List<ProjectStage> ccs = DbTemplate.queryForList(selSql, new Object[]{n}, ProjectStage.class);
			CacheUtils.addEleByParam(CacheUtils.PROJ_CACHE, "newProStage"+n, ccs);
			return ccs;
		}
	}

	@Override
	public List<ProjectStage> selProStageByProjectCache(String projectId) {
		
		String selSql = " select * from project_stage where bc_project_id = ? order by project_stage_id  ";
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.PROJ_CACHE,  "projectStagesList"+projectId);
		if(dbEle != null){
			return (List<ProjectStage>)dbEle;
		}else{
			List<ProjectStage> ccs = DbTemplate.queryForList(selSql, new Object[]{projectId}, ProjectStage.class);
			CacheUtils.addEleByParam(CacheUtils.PROJ_CACHE, "projectStagesList"+projectId, ccs);
			return ccs;
		}
	}

	@Override
	public void updProStageViewCount(String id, int count) {
		String updSql = " update project_stage set view_count = view_count + ? where project_stage_id = ?  ";
		DbTemplate.update(updSql, new Object[]{count,id});
	}
}
